package com.example.voluntary.mapper;

import com.example.voluntary.domain.dto.ActivityAuditDTO;
import com.example.voluntary.domain.dto.ActivityQueryDTO;
import com.example.voluntary.domain.po.ActivityDoc;
import com.example.voluntary.domain.po.VolunteerActivities;
import com.example.voluntary.domain.vo.CreatedActivityVO;
import com.example.voluntary.domain.vo.ParticipatedActivityVO;
import com.example.voluntary.domain.vo.VoluntaryManagementVO;
import com.example.voluntary.domain.vo.VoluntaryVO;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface VolunteerActivitiesMapper {
    @Insert("INSERT INTO volunteer_activities (tree_id, title, description, " +
            "activity_image, activity_type, start_time, end_time, location, " +
            "max_participants, current_participants, status, user_id, created_at)" +
            " VALUES (#{treeId}, #{title}, #{description}, #{activityImage}," +
            " #{activityType}, #{startTime}, #{endTime}, #{location}, #{maxParticipants}," +
            " #{currentParticipants}, #{status}, #{userId}, #{createdAt})")
    void insert(VolunteerActivities volunteerActivities);

    /**
     * 查询志愿活动列表
     * @param pageNum 页码
     * @param size 每页数量
     * @return 志愿活动列表
     */
    @Select("SELECT  activity_id, title, activity_id, activity_type,activity_image," +
            "start_time,end_time,status from volunteer_activities where audit_status=1" )
    Page<VoluntaryVO> selectVoluntaryList(Integer pageNum, Integer size);

    /**
     * 根据id查询志愿活动
     * @param activityId 志愿活动id
     * @return 志愿活动
     */
    @Select("SELECT * from volunteer_activities where activity_id = #{activityId}")
     VolunteerActivities selectById(Integer activityId);

    /**
     * 查询所有志愿活动
     * @return 志愿活动列表
     */
    @Select("SELECT activity_id,title,activity_image, start_time,end_time,status from volunteer_activities")
    List<ActivityDoc> selectActivityDocList();

    /**
     * 获取志愿活动管理列表
     * @param queryDTO 查询参数
     * @return 志愿活动列表
     */
    Page<VoluntaryManagementVO> selectVoluntaryManagementList(ActivityQueryDTO queryDTO);

   /**
     * 审核志愿活动
     * @param volunteerActivities 审核参数
     */
    void auditVoluntaryActivity(VolunteerActivities volunteerActivities);

    void updateActivity(VolunteerActivities activities);

    @Update("UPDATE volunteer_activities SET current_participants = current_participants + 1 WHERE activity_id = #{activityId}" +
            " AND current_participants < max_participants")
    int updateActivityCurrentParticipants(Integer activityId);


    List<CreatedActivityVO> selectCreatedActivitiesByUserId(Long userId);

    List<ParticipatedActivityVO> selectParticipatedActivitiesByUserId(Long userId);

}
